package com.kelvin.paopao.module.main.date.publish.other;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.airsaid.pickerviewlibrary.TimePickerView;
import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.injector.components.DaggerPublishOtherComponent;
import com.kelvin.paopao.injector.modules.PublishOtherModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.main.date.publish.eat.PublicEatActivity;
import com.kelvin.paopao.module.main.date.publish.editlocation.EditLocationActivity;
import com.kelvin.paopao.module.qrcode.webview.ScanWebViewActivity;
import com.kelvin.paopao.utils.AnimateHelper;
import com.kelvin.paopao.widgets.CustomToolbar;
import com.kelvin.paopao.widgets.PublishImageEditView;
import com.kelvin.paopao.widgets.dialog.OptionSelector;
import com.lwkandroid.imagepicker.ImagePicker;
import com.lwkandroid.imagepicker.data.ImageBean;
import com.lwkandroid.imagepicker.data.ImagePickType;
import com.lwkandroid.imagepicker.utils.GlideImagePickerDisplayer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by MCK on 2017/5/10.
 */

public class PublishOtherActivity extends BaseActivity<IPublishOtherPresenter> {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.et_theme)
    EditText etTheme;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.rg_gender)
    RadioGroup rgGender;
    @BindView(R.id.rg_pay_bill)
    RadioGroup rgPayBill;
    @BindView(R.id.et_introduce)
    EditText etIntroduce;
    @BindView(R.id.recycler_view)
    PublishImageEditView recyclerView;
    @BindView(R.id.iv_pay_switch)
    ImageView ivPaySwitch;
    @BindView(R.id.et_pay_reward)
    EditText etPayReward;
    @BindView(R.id.et_hour)
    EditText etHour;
    @BindView(R.id.ll_reward_hide)
    LinearLayout llRewardHide;
    @BindView(R.id.tv_location)
    TextView tvLocation;


    private String gender, payMode;

    public static final int REQUEST_CODE_FOR_IMG = 1001;
    private static final int REQUEST_CODE_FOR_LOCATION = 1002;
    private static final int REQUEST_CODE_FOR_EDIT_LOCATION = 1003;

    private String oAddressId;


    private String location, time, payRewardPH = "0", hours = "0", introduce, theme;
    private boolean isTip;

    /**
     * 时间选择器
     */
    private TimePickerView mTimePickerView;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_publish_other;
    }

    @Override
    protected void initInjector() {
        DaggerPublishOtherComponent.builder().publishOtherModule(new PublishOtherModule(this))
                .build().inject(this);
    }


    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);
        customToolbar.inflateMenu(R.menu.menu_publish);
        customToolbar.setMenuOnClick(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                publish();
                return true;
            }
        });

        rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_male) {
                    gender = "男";
                } else if (checkedId == R.id.rb_female) {
                    gender = "女";
                } else {
                    gender = "不限";
                }
            }
        });

        rgPayBill.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_aa) {
                    payMode = "AA";
                } else {
                    payMode = "我买单";
                }
            }
        });

        recyclerView.create(this);
        recyclerView.setMaxCount(3);
        recyclerView.setListener(new PublishImageEditView.AddImgListner() {
            @Override
            public void addImage() {
                pickImage();
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, PublishOtherActivity.class);
        context.startActivity(intent);
    }

    @OnClick({R.id.ll_select_time, R.id.ll_pay_reward, R.id.ll_location})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_select_time:
                CommonUtil.HideKeyboard(etTheme);
                showTimePickDialog();
                break;
            case R.id.ll_location:
                CommonUtil.HideKeyboard(etTheme);
                OptionsPickerView pickerView = OptionSelector.showLocationSelector(PublishOtherActivity.this);
                pickerView.setOnOptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int option1, int option2, int option3) {
                        if (option1 == 0) {
                            ScanWebViewActivity.launch(PublishOtherActivity.this, REQUEST_CODE_FOR_LOCATION,
                                    ScanWebViewActivity.TYPE_QITA);
                        } else {
                            EditLocationActivity.launch(PublishOtherActivity.this, REQUEST_CODE_FOR_EDIT_LOCATION);
                        }
                    }
                });
                break;
            case R.id.ll_pay_reward:
                if (llRewardHide.getVisibility() == View.VISIBLE) {
                    ivPaySwitch.setImageResource(R.drawable.icon_off);
                    AnimateHelper.llClose(PublishOtherActivity.this, llRewardHide);
                    isTip = false;
                } else {
                    ivPaySwitch.setImageResource(R.drawable.icon_on);
                    AnimateHelper.llOpen(PublishOtherActivity.this, llRewardHide);
                    isTip = true;
                }
                break;
        }
    }

    /**
     * 进入图片选择器
     */
    private void pickImage() {
        //发起图片选择
        new ImagePicker.Builder()
                .pickType(ImagePickType.SINGLE) //设置选取类型(拍照ONLY_CAMERA、单选SINGLE、多选MUTIL)
                .needCamera(true) //是否需要在界面中显示相机入口(类似微信那样)
                .displayer(new GlideImagePickerDisplayer()) //自定义图片加载器，默认是Glide实现的,可自定义图片加载器
                .build()
                .start(this, REQUEST_CODE_FOR_IMG, Activity.RESULT_OK); //自定义RequestCode和ResultCode
    }

    private void publish() {
        location = tvLocation.getText().toString();
        time = tvTime.getText().toString();
        payRewardPH = etPayReward.getText().toString();
        hours = etHour.getText().toString();
        introduce = etIntroduce.getText().toString();
        String oDate = tvTime.getText().toString();

        if (TextUtils.isEmpty(hours)) {
            hours = "0";
        }
        if (TextUtils.isEmpty(payRewardPH)) {
            payRewardPH = "0";
        }

        if (TextUtils.isEmpty(location) || time.equals("选择时间") ||
                TextUtils.isEmpty(introduce)) {
            ToastUtil.showToast(this, "填写完整再发布哦~~");
            return;
        }
        if (isTip) {
            if (TextUtils.isEmpty(payRewardPH) || TextUtils.isEmpty(hours)) {
                ToastUtil.showToast(this, "填写完整再发布哦~~");
                return;
            }
        }

        mPresenter.publishOther(location, theme, time, gender, payMode
                , isTip ? "1" : "0", payRewardPH, hours, introduce,
                recyclerView.getPositionImage(0), recyclerView.getPositionImage(1),
                recyclerView.getPositionImage(2), oAddressId, oDate);
    }

    /**
     * 展示日期选择器
     */
    private void showTimePickDialog() {
        mTimePickerView = new TimePickerView(this, TimePickerView.Type.MONTH_DAY_HOUR_MIN);
        mTimePickerView.setTime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        mTimePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA);
                String strDate = format.format(date);
                tvTime.setText(strDate);
            }
        });
        mTimePickerView.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_FOR_IMG) {
            List<ImageBean> resultList = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA);
            recyclerView.addImageView(resultList.get(0).getImagePath());
        }
        if (requestCode == REQUEST_CODE_FOR_LOCATION) {
            oAddressId = data.getStringExtra("id");
            tvLocation.setText(data.getStringExtra("name"));
        }
        if (requestCode == REQUEST_CODE_FOR_EDIT_LOCATION) {
            if (data!=null&&!TextUtils.isEmpty(data.getStringExtra("data"))) {
                tvLocation.setText(data.getStringExtra("data"));
            }
        }
    }

}
